package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.mapper.IsolationPositivePersonnelMapper;
import com.lnking.lnkingplace.model.dto.isolation.positivePersonnel.PositivePersonnelImport;
import com.lnking.lnkingplace.model.entity.IsolationPositivePersonnel;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.request.isolation.positivePersonnel.PositivePersonnelAddRequest;
import com.lnking.lnkingplace.model.request.isolation.positivePersonnel.PositivePersonnelUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationPositivePersonnelService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-21
 */
@Service
public class IsolationPositivePersonnelServiceImpl extends CustomServiceImpl<IsolationPositivePersonnelMapper, IsolationPositivePersonnel> implements IIsolationPositivePersonnelService {

    @Resource
    IsolationPlaceMapper isolationPlaceMapper;

    @Override
    public void importExcel(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<PositivePersonnelImport> result =  ImportUtils.importExcel(file, PositivePersonnelImport.class,option);
        result.forEach(r ->{
            IsolationPositivePersonnel info = new IsolationPositivePersonnel();
            BeanUtils.copyProperties(r,info);
            //查询placeId
            String placeId = isolationPlaceMapper.getPlaceId(r.getPlaceName());
            Assert.isTrue(StringUtils.isNotBlank(placeId), "该场所不存在："+"【"+r.getPlaceName()+"】");
            info.setPlaceId(placeId);
            info.setCreateTime(LocalDateTime.now());
            info.setAge(Integer.parseInt(r.getAge()));
            info.setConfirmedSamplingDate(LocalDateTimeUtil.stringToLocalDateTime(r.getConfirmedSamplingTime(),DateFormat.LOCAL_DATE_TIME));
            info.setConfirmedDate(LocalDateTimeUtil.stringToLocalDateTime(r.getConfirmedTime(),DateFormat.LOCAL_DATE_TIME));
            info.setTransitDate(LocalDateTimeUtil.stringToLocalDateTime(r.getTransitTime(),DateFormat.LOCAL_DATE_TIME));
            info.setKillDate(LocalDateTimeUtil.stringToLocalDateTime(r.getKillTime(),DateFormat.LOCAL_DATE_TIME));
            info.setEnvironmentalSamplingDate(LocalDateTimeUtil.stringToLocalDateTime(r.getEnvironmentalSamplingTime(),DateFormat.LOCAL_DATE_TIME));
            info.setDeliverDate(LocalDateTimeUtil.stringToLocalDateTime(r.getDeliverDate(),DateFormat.LOCAL_DATE));
            if(StringUtils.isNotBlank(r.getStartDate())){
                LocalDateTime startTime = LocalDateTimeUtil.stringToLocalDateTime(r.getStartDate() + " 00:00:00",DateFormat.LOCAL_DATE_TIME);
                info.setCreateTime(startTime);
            }
            this.baseMapper.insert(info);
        });
    }

    @Override
    public void add(PositivePersonnelAddRequest params) {
        IsolationPositivePersonnel info = new IsolationPositivePersonnel();
        BeanUtils.copyProperties(params,info);
        info.setCreateTime(LocalDateTime.now());
        String placeId = isolationPlaceMapper.getPlaceId(params.getPlaceName());
        info.setPlaceId(placeId);
        this.baseMapper.insert(info);
    }

    @Override
    public void update(PositivePersonnelUpdateRequest params) {
        IsolationPositivePersonnel info = new IsolationPositivePersonnel();
        BeanUtils.copyProperties(params,info);
        info.setCreateTime(LocalDateTime.now());
        String placeId = isolationPlaceMapper.getPlaceId(params.getPlaceName());
        info.setPlaceId(placeId);
        this.baseMapper.updateById(info);
    }
}
